/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.control;

import com.scalia.meteocal.entity.Event;
import com.scalia.meteocal.entity.Group;
import com.scalia.meteocal.entity.Invitation;
import com.scalia.meteocal.entity.Notification;
import com.scalia.meteocal.entity.User;
import com.scalia.meteocal.entity.WeatherType;
import java.security.Principal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author gabriele
 */
@Stateless
public class EventManager {
    
    @PersistenceContext
    EntityManager em;
    
    @EJB
    UserManager userManager;
   
    @EJB
    NotificationManager notificationManager;
    
    public Event newEvent(Event event, String[] badWeatherTypes) {
        if (badWeatherTypes==null) {
            String[] defaultWeatherTypes = {WeatherType.BROKEN_CLOUDS.getName(), WeatherType.SHOWER_RAIN.getName(), WeatherType.RAIN.getName(), WeatherType.SNOW.getName(),
                WeatherType.MIST.getName(), WeatherType.THUNDERSTORM.getName()};  
            event.setBadWeatherTypes(badWeatherTypesEncoder(defaultWeatherTypes));
        }
        else {
            event.setBadWeatherTypes(badWeatherTypesEncoder(badWeatherTypes));
        }
        event.setDeleted(false);
        em.persist(event);
        em.flush();
        return event;
    }
    
    public void modifyEvent(Event event) {
        em.merge(event);
        notificationManager.eventModified(event);
    }
    
    public void deleteEvent(Event event) {
        event = em.merge(event);
        Query query = em.createNamedQuery(Invitation.findAllInvitationsOfEvent).setParameter("event", event);
        List<Invitation> relatedInvitations = query.getResultList();
        for (Invitation invitation : relatedInvitations) {
            invitation = em.merge(invitation);
            em.remove(invitation);
        }
        
        event.setDeleted(true);
        em.merge(event);
        em.flush();
        notificationManager.eventDeleted(event);     
    }
    
    public String badWeatherTypesEncoder(String[] badWeatherTypes) {
        String result = new String();
        for (String s : badWeatherTypes) 
            result = result.concat(s + "$");
        return result;
    }
    
    public String[] badWeatherTypesDecoder(String badWeatherTypes) {
        if (badWeatherTypes.isEmpty())
            return new String[0];
        return badWeatherTypes.split("\\$");
    }
    
    public WeatherType[] getBadWeatherTypes(String[] badWeatherTypesString) {
        WeatherType[] result = new WeatherType[badWeatherTypesString.length];
        for (int i=0; i<result.length; i++)
            result[i] = getBadWeatherType(badWeatherTypesString[i]);
        return result;
    }
    
    public WeatherType[] getBadWeatherTypes(String badWeatherTypesString) {
        return getBadWeatherTypes(badWeatherTypesDecoder(badWeatherTypesString));
    }
    
    public WeatherType getBadWeatherType(String badWeatherTypeString) {
        for (WeatherType weatherType : WeatherType.values()) {
            if (weatherType.getName().equals(badWeatherTypeString)) {
                return weatherType;
            }
        }
        return null;
    }
     
    public List<Event> getAllEvents() {
        Query query = em.createNamedQuery(Event.findAll);
        return query.getResultList();
    }
    
    public List<Event> getAllEventsOfUser(User user) {
        Query query = em.createNamedQuery(Event.findAllOfUser).setParameter("user", user);
        return query.getResultList();
    }
    
    public List<Event> getAllMyEvents() {
        return getAllEventsOfUser(userManager.getLoggedUser());
    }
    
    public Event getEvent(Long id) {
        return em.find(Event.class, id);
    }
    
    public List<Event> getAllMyPartecipations() {
        return getAllPartecipationsOfUser(userManager.getLoggedUser());
    }
    
    public List<Event> getAllPartecipationsOfUser(User user) {
        Query query = em.createNamedQuery(Event.findAllPartecipationsOfUser).setParameter("user", user);
        return query.getResultList();
    }
   
}
